//
//  TestEqualDivisionLayoutConstraint.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2025/7/11.
//  Copyright © 2025 moon. All rights reserved.
//

#import "TestEqualDivisionLayoutConstraint.h"
#import "Menu.h"
#import "TestElementHeaders.h"

@interface TestEqualDivisionView : MKUIListViewSubViewBase
@property(nonatomic,strong) MKUIEqualDivisionLayoutConstraint *flowlayout;
- (void)addItem:(UIView *)view;
- (void)deleteItem:(UIView *)view;
@end
@implementation TestEqualDivisionView
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.flowlayout = [[MKUIEqualDivisionLayoutConstraint alloc] initWithItems:nil constraintParam:(MKUIEqualDivisionLayoutConstraintParam_H_C_C) contentInsets:MKUIEdgeInsetsMakeSameEdge(10) interitemSpacing:10];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.contentView.bounds;
    self.flowlayout.bounds = bounds;
    [self.flowlayout layoutItemsWithResizeItems:YES];
}
- (CGSize)sizeThatFits:(CGSize)size{
    CGSize s = [self.flowlayout sizeThatFits:CGSizeMake(size.width, 200) resizeItems:YES];
    return s;
}
- (void)addItem:(UIView *)view{
    if(!view)return;
    [self.contentView addSubview:view];
    [self.flowlayout addItem:view];
    [self setNeedsLayout];
    [self mk_reloadInListView];
}
- (void)deleteItem:(UIView *)view{
    if(!view) return;
    [view removeFromSuperview];
    [self.flowlayout removeItem:view];
    [self setNeedsLayout];
    [self mk_reloadInListView];
}
@end

@interface TestEqualDivisionLayoutConstraint ()
@property(nonatomic,strong) TestEqualDivisionView *viewsElement;
@end

@implementation TestEqualDivisionLayoutConstraint

+ (MKTFuncItem *)funcItem{
    return [MKTFuncItem funcWithName:@"EqualDivision布局约束" viewControllerClass:self];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self reloadData];
}
- (void)reloadData{
    @MK_WEAKIFY(self);
    [self.listView deleteAllSections];
    //
    [self.listView addSection];
    self.viewsElement = [[TestEqualDivisionView alloc] init];
    [self.listView addItem:self.viewsElement];
    MKUIEqualDivisionLayoutConstraint *flowlayout = self.viewsElement.flowlayout;
    //
    [self.listView addSection];
    [self.listView addItem:[MKUIElementListViewCellSelection testElementMKUIEqualDivisionLayoutConstraintParam:@"constraintParam" selected:flowlayout.constraintParam whenDone:^(MKUIEqualDivisionLayoutConstraintParam selectedValue) {
        @MK_NORMALIZE(self);
        flowlayout.constraintParam = selectedValue;
        [self.viewsElement mk_reloadInListView];
    }]];
    [self.listView addItem:[TestMKUIElementViewSlider_UIEdgeInsets testSliderElementWithTitle:@"contentInsets" value:flowlayout.contentInsets minimumValue:UIEdgeInsetsZero maximumValue:UIEdgeInsetsMake(20, 20, 20, 20) whenChange:^(TestMKUIElementViewSlider_UIEdgeInsets * _Nonnull el, UIEdgeInsets value) {
        @MK_NORMALIZE(self);
        flowlayout.contentInsets = value;
        [self.viewsElement mk_reloadInListView];;
    }]];
    [self.listView addItem:[TestMKUIElementViewSlider testSliderElementWithTitle:@"interitemSpacing" value:flowlayout.interitemSpacing minimumValue:0 maximumValue:20 whenChange:^(TestMKUIElementViewSlider * _Nonnull el, CGFloat value) {
        @MK_NORMALIZE(self);
        flowlayout.interitemSpacing = value;
        [self.viewsElement mk_reloadInListView];;
    }]];
    [self.listView addItem:[MKUIElementListViewCellSwitch testSwitchElementWithTitle:@"itemFillCellBounds" on:flowlayout.itemFillCellBounds whenChange:^(BOOL on) {
        @MK_NORMALIZE(self);
        flowlayout.itemFillCellBounds = on;
        [self.viewsElement mk_reloadInListView];;
    }]];
    [self.listView addItem:[MKUIElementListViewCellSwitch testSwitchElementWithTitle:@"fixSizeToFitsBounds" on:flowlayout.fixSizeToFitsBounds whenChange:^(BOOL on) {
        @MK_NORMALIZE(self);
        flowlayout.fixSizeToFitsBounds = on;
        [self.viewsElement mk_reloadInListView];;
    }]];
    //
    [self.listView addSection];
    [self.listView addItem:[MKUIElementListViewCellButton testButtonElementWithTitle:@"添加小图" buttonStyle:(MKUIElementViewButtonStyleDefault) whenClick:^(MKUIElementListViewCellButton * _Nonnull el) {
        @MK_NORMALIZE(self);
        MKUIFlowLayoutButton *barButton = [self createButton];
        barButton.imageSize = CGSizeMake(20, 20);
        [self.viewsElement addItem:barButton];
        [self.viewsElement mk_reloadInListView];
    }]];
    [self.listView addItem:[MKUIElementListViewCellButton testButtonElementWithTitle:@"添加大图" buttonStyle:(MKUIElementViewButtonStyleDefault) whenClick:^(MKUIElementListViewCellButton * _Nonnull el) {
        @MK_NORMALIZE(self);
        MKUIFlowLayoutButton *barButton = [self createButton];
        barButton.imageSize = CGSizeMake(40, 40);
        [self.viewsElement addItem:barButton];
        [self.viewsElement mk_reloadInListView];
    }]];
    {
        MKUIFlowLayoutButton *barButton = [self createButton];
        barButton.imageSize = CGSizeMake(20, 20);
        [self.viewsElement addItem:barButton];
        [self.viewsElement mk_reloadInListView];
    }
    {
        MKUIFlowLayoutButton *barButton = [self createButton];
        barButton.imageSize = CGSizeMake(40, 40);
        [self.viewsElement addItem:barButton];
        [self.viewsElement mk_reloadInListView];
    }
    //
    [self.listView reloadData];
}
- (MKUIFlowLayoutButton *)createButton{
    MKUIFlowLayoutButton *barButton = [[MKUIFlowLayoutButton alloc] init];
    barButton.imageSize = CGSizeMake(40, 40);
    [barButton setImage:Menu.randomImage forState:(UIControlStateNormal)];
    @MK_WEAKIFY(barButton);
    [barButton mk_addClickActionBlock:^(TestEqualDivisionLayoutConstraint *self) {
        @MK_NORMALIZE(barButton);
        [self.viewsElement deleteItem:barButton];
        [self.viewsElement mk_reloadInListView];;
    } context:self];
    barButton.layer.borderColor = UIColor.blueColor.CGColor;
    barButton.layer.borderWidth = 1;
    return barButton;
}
@end
